/**
 * 全站权限配置
 *
 */
import router from './router/router'
import store from './store'
import {getToken} from '@/util/auth'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css'
import website from "@/config/website";
import {request} from "axios"; // progress bar style
NProgress.configure({showSpinner: false});
router.beforeEach((to, from, next) => {
    if (to.path === "/workflowView") {
        next()
    } else {
        if (!getToken() && !window.location.href.includes('code')) {
            if (process.env.NODE_ENV === 'development') {
                window.open(process.env.VUE_APP_LOGIN_URL + website.ssoUrl + website.redirectUri, "_self")
            } else {
                request({url: "/api/manager-api/url/getAuthServiceUrl", method: "get"}).then(res => {
                    let baseUrl = res.data.split(",")[1];
                    window.open(baseUrl + website.ssoUrl + website.redirectUri, "_self")
                })
            }
        } else {
            if (to.path === '/') {
                next("/loading")
            } else {
                const value = to.query.src || to.fullPath;
                let label = to.query.name || to.name;
                const meta = to.meta || router.$avueRouter.meta || {};
                const i18n = to.query.i18n;

                let arr = [];

                function flattenArray(v) {
                    if (v && v.length > 0) {
                        for (let i = 0; i < v.length; i++) {
                            if (v[i].hasChildren) {
                                flattenArray(v[i].children)
                            } else {
                                arr.push(v[i])
                            }
                        }
                    }
                }

                flattenArray(store.getters.menu)

                arr.forEach(i => {
                    if (i.path === value) {
                        label = i.name;
                    }
                })

                if (to.path !== "/loading") {
                    store.commit('ADD_TAG', {
                        label: label,
                        value: value,
                        params: to.params,
                        query: to.query,
                        meta: (() => {
                            if (!i18n) {
                                return meta
                            }
                            return {
                                i18n: i18n
                            }
                        })(),
                        group: router.$avueRouter.group || []
                    });
                }
                next()
            }
        }
    }
})

/*router.afterEach(() => {
  NProgress.done();
  let title = store.getters.tag.label;
  let i18n = store.getters.tag.meta.i18n;
  title = router.$avueRouter.generateTitle(title, i18n);
  //判断登录页的情况
  if (router.history.current.fullPath === "/login") {
    title = "登录";
  }
  //根据当前的标签也获取label的值动态设置浏览器标题
  router.$avueRouter.setTitle(title);
});*/
